﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BTM_Client_Application
{
    /// <summary>
    /// Логика взаимодействия для ClientEventsControl.xaml
    /// </summary>
    public partial class ClientEventsControl : UserControl
    {

        private readonly ObservableCollection<ClientEventItem> _itemsList;
        private Button _but;
        private int _unsolvedCount = 0;

        private ClientEventsControl()
        {
            InitializeComponent();
            _itemsList = new ObservableCollection<ClientEventItem>();
            ContentList.ItemsSource = _itemsList;
        }

        public static ClientEventsControl CreateControl(Button but)
        {
            return new ClientEventsControl {_but = but};
        }

        public ClientEventItem AddEvent(string title, string info)
        {
            ClientEventItem ev = null;
            Dispatcher.Invoke(new Action(() =>
                {
                    try
                    {
                        ev =
                            _itemsList.First(
                                e =>
                                e.State == ClientEventItem.EventState.Unsolved && e.Title == title && e.Info == info);
                        //return ev;
                    }
                    catch (Exception)
                    {
                    }

                    ev = new ClientEventItem(title, info);
                    _itemsList.Add(ev);
                    if (!_but.IsVisible)
                    {
                        _but.Visibility = Visibility.Visible;
                        _but.Tag = false;
                    }
                    ev.StateChangedEvent += CviOnStateChangedEvent;
                    //return cvi;
                }));
            return ev;
        }

        public bool RemoveEvent(string title, string info)
        {
            int i = _itemsList.TakeWhile(item => !item.CompareInfo(title, info)).Count();
            if (i < _itemsList.Count)
            {
                _itemsList.RemoveAt(i);
                CviOnStateChangedEvent(true);
                return true;
            }
            return false;
        }

        private void CviOnStateChangedEvent(bool b)
        {
            if (b)
            {
                _unsolvedCount--;
                if (_unsolvedCount < 1)
                {
                    _but.Tag = true;
                }
            }
        }

        private void ContentList_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = ContentList.SelectedItem as ClientEventItem;
            RemoveEvent(item.Title, item.Info);
            //var it = _itemsList.Single(x => x.Title == item.Title && x.Info == item.Info);
            //_itemsList.Remove(it);

        }
    }
}
